//
//  DDPlayMusicViewController.m
//  DDMusic
//
//  Created by rimi on 16/11/10.
//  Copyright © 2016年 FengBaoFamily. All rights reserved.
//

#import "DDPlayMusicViewController.h"
#import <Masonry.h>
#import "DDMusicProgressView.h"
#import "UILabel+DDUILabelCategory.h"
#import "DDPublicMacro.h"
#import <UIImageView+WebCache.h>
#import "DDAlertView.h"
#import "DDMainTabBar.h"
#import <UIButton+WebCache.h>
#import "CALayer+PauseAimate.h"
#import "CMuneBar.h"
#import "UIImage+DDUiimageCategory.h"
#import "DDmediaPlayer.h"
#pragma mark 播放顺序的枚举
typedef NS_ENUM(NSInteger, MusicPlaySequence) {
    DEFAULT,//默认
    SINGLELOOPPLAY,//单曲循环
    ALLLOOPPLAY,//循环播放
    RANDOMPLAY,//随机播放
    
    
};
@interface DDPlayMusicViewController ()<CMuneBarDelegate>
#pragma mark UI相关的属性
@property (nonatomic,strong) UIImageView* bgImageView;//背景图片
@property (nonatomic,strong) UILabel* songNameLabel;//歌曲名称
@property (nonatomic,strong) UILabel* singerNameLabel;//歌手名
@property (nonatomic,strong) DDMusicProgressView* progressView;//歌曲进度条
@property (nonatomic,strong) UIButton* upBtn;//上一首按钮
@property (nonatomic,strong) UIButton* musicListBtn;//歌单列表按钮
@property (nonatomic,strong) CMuneBar *musicToolBar; //工具按钮
@property (nonatomic,strong) UIButton* toolBarBtn;//工具按钮
#pragma mark 播放相关的属性
@property (nonatomic,strong) AVPlayerItem* item;
@property (nonatomic,assign) NSInteger currentPlayMusicIndex;//当前播放歌曲在歌曲列表里的下标;
@property (nonatomic,strong) NSTimer* playMusicTimer;//跟进歌曲播放进度的timer 用于更新滑条进度 切换下一首歌曲的timer
@property (nonatomic,assign) MusicPlaySequence playSequence;//播放方式

@end

@implementation DDPlayMusicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self layoutUI];
    self.playMusicList = [NSMutableArray array];
    [self addObserver:self forKeyPath:@"playMusicList" options:NSKeyValueObservingOptionNew context:nil];//给歌曲列表添加kvo,随时根据播放列表更新ui和播放歌曲
    [self registerCenter];
    self.playSequence = DEFAULT;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear: animated];
 [self  acceptlongRangeControl];
 

}
- (void)viewWillDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
    [self touchLongRangeControl];
    [self.musicToolBar hideItems];
}
- (void)dealloc{
    [self removeObserver:self forKeyPath:@"playMusicList"];

    [[NSNotificationCenter defaultCenter]removeObserver:self];
    
}
#pragma mark UI部分
- (void)layoutUI{
//bgImageView
    self.bgImageView = [[UIImageView alloc]init];
    [self.view addSubview:self.bgImageView];
    [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(@0);
    make.right.equalTo(@0);
    make.top.equalTo(@0);
    make.bottom.equalTo(@0);
    
    
    }];

//返回按钮
    UIButton* disMissBtn = [[UIButton alloc]init];
    [disMissBtn setBackgroundImage:[UIImage imageNamed:@"dissMiss"] forState:(UIControlStateNormal)];
    [disMissBtn addTarget:self action:@selector(dismissBtnClicked) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:disMissBtn];
    [disMissBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@8);
        make.top.equalTo(@30);
        make.height.width.equalTo(@25);
    }];
//songNameLabel
    self.songNameLabel = [UILabel dd_CreatLabelWithTitle:@"" font:19 TitleColor:[UIColor whiteColor] TextAlignment:(NSTextAlignmentCenter)];
    [self.view addSubview:self.songNameLabel];
  __weak  DDPlayMusicViewController* weakSelf = self;
    [self.songNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.view.mas_centerX);
        make.top.equalTo(@25);
        make.left.equalTo(@50);
        make.right.equalTo(@-50);
    }];
//singerNameLabel
    self.singerNameLabel = [UILabel dd_CreatLabelWithTitle:@"" font:16 TitleColor:[UIColor whiteColor] TextAlignment:(NSTextAlignmentCenter)];
    [self.view addSubview:self.singerNameLabel];
    [self.singerNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.view.mas_centerX);
        make.top.equalTo(weakSelf.songNameLabel.mas_bottom).offset(4);
      
    
    }];
//progressView
    self.progressView = [[DDMusicProgressView alloc]init];
    self.progressView.progressStyle = MusicProgressDefaultStyle;
    [self.progressView refreshUI:[UIImage imageNamed:@"circle"] minimumTrackTintColor:[UIColor whiteColor] maximumTrackTintColor:[UIColor colorWithRed:154/255.0 green:152/255.0 blue:157/255.0 alpha:1]];
    [self.progressView.musicSider addTarget:self action:@selector(sliderMusicProgress) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.progressView];
    [self.progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@8);
        make.right.equalTo(@8);
        make.height.equalTo(@10);
        make.bottom.equalTo(weakSelf.view.mas_bottom).offset(-(SCREEN_SIZE.height* 0.12));
        
    }];
    
//stopOrPauseBtn
    self.stopOrPauseBtn = [[UIButton alloc]init];
    [self.stopOrPauseBtn setBackgroundImage:[UIImage imageNamed:@"purpleColorStop"] forState:(UIControlStateSelected)];
    [self.stopOrPauseBtn setBackgroundImage:[UIImage imageNamed:@"purplrColorPlay"] forState:(UIControlStateNormal)];
    self.stopOrPauseBtn.layer.cornerRadius = (SCREEN_SIZE.height*0.12-20)/2;
    self.stopOrPauseBtn.layer.masksToBounds = YES;
    [self.stopOrPauseBtn addTarget:self action:@selector(playNextOrUpBtnClicked:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:self.stopOrPauseBtn];
    [self.stopOrPauseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(@(SCREEN_SIZE.height*0.12-20));
        make.centerX.equalTo(weakSelf.view.mas_centerX);
        make.bottom.equalTo(@(-10));
    }];
//nextBtn
    self.nextBtn = [[UIButton alloc]init];
    [self.nextBtn setBackgroundImage:[UIImage imageNamed:@"purpleColorNext"] forState:(UIControlStateNormal)];
    [self.nextBtn addTarget:self action:@selector(playNextOrUpBtnClicked:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:self.nextBtn];
    [self.nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.stopOrPauseBtn.mas_centerY);
        make.width.height.equalTo(weakSelf.stopOrPauseBtn.mas_width).offset(-30);
        make.left.equalTo(weakSelf.stopOrPauseBtn.mas_right).offset(20);
    }];
//upBtn
    self.upBtn = [[UIButton alloc]init];
    [self.upBtn setBackgroundImage:[UIImage imageNamed:@"purpleColorUp"] forState:(UIControlStateNormal)];
    [self.upBtn addTarget:self action:@selector(playNextOrUpBtnClicked:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:self.upBtn];
    [self.upBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.stopOrPauseBtn.mas_centerY);
        make.width.height.equalTo(weakSelf.stopOrPauseBtn.mas_width).offset(-30);
        make.right.equalTo(weakSelf.stopOrPauseBtn.mas_left).offset(-20);
    }];
//musicToolBar
    self.musicToolBar = [[CMuneBar alloc]initWithItems:@[@"playStyle",@"disLike",@"barrage",@"download",@"settings",@"gift"] size:CGSizeMake(50, 50) type:(kMuneBarTypeLineTop)];
    //设置收藏按钮的选中模式
    [self.musicToolBar.items[1] setImage:[UIImage imageNamed:@"like"] forState:(UIControlStateSelected)];
    self.musicToolBar.delegate = self;
    [self.view addSubview:self.musicToolBar];
    [self.musicToolBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.stopOrPauseBtn.mas_centerY);
        make.width.height.equalTo(@50);
        make.left.equalTo(@10);
    }];
//musicListBtn
    self.musicListBtn = [[UIButton alloc]init];
    [self.musicListBtn setImage:[[UIImage imageNamed:@"list"] dd_zoomImageToSize:CGSizeMake(20, 20)]forState:(UIControlStateNormal)];
    [self.view addSubview:self.musicListBtn];
    [self.musicListBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.stopOrPauseBtn.mas_centerY);
        make.width.height.equalTo(@50);
        make.right.equalTo(@(-10));
    }];
}
#pragma mark 逻辑操作
#pragma mark 返回按钮的回调方法
- (void)dismissBtnClicked{
    
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark 播放,上一首，下一首按钮被点击的回调方法
- (void)playNextOrUpBtnClicked:(UIButton*)button{
    if (button == self.stopOrPauseBtn) {
        //播放暂停按钮
        button.selected = !button.selected;
        if (button.selected == YES) {
            [self.player play];
            self.playMusicTimer.fireDate = [NSDate distantPast];
            [DDMainTabBar sharedDDMainTabBar].statrOrStopBtn.selected = YES;
            [[DDMainTabBar sharedDDMainTabBar].playingMusicBtn.layer resumeAnimate];
        }else{
            [self.player pause];
            self.playMusicTimer.fireDate = [NSDate distantFuture];
            [DDMainTabBar sharedDDMainTabBar].statrOrStopBtn.selected = NO;
            [[DDMainTabBar sharedDDMainTabBar].playingMusicBtn.layer pauseAnimate];
        }
    }else if (button == self.nextBtn){
//下一首按钮
        if (self.playMusicList.count <=0) {
           
            return;
        }
        switch (self.playSequence) {
            case DEFAULT:
                if (self.playMusicList.count>self.currentPlayMusicIndex+1) {
                    self.currentPlayModel = self.playMusicList[self.currentPlayMusicIndex+1];
                    [self refreshUI:self.currentPlayModel];
                    [self playMusic:self.currentPlayModel];
                    self.currentPlayMusicIndex+=1;
                }else if (self.player.status == AVPlayerStatusReadyToPlay){
                }
                else{
                    self.progressView.musicSider.value = 0;
                    self.playMusicTimer.fireDate = [NSDate distantFuture];
                    self.progressView.currentTimeLabel.text = @"00:00";
                    self.stopOrPauseBtn.selected = NO;
                    [DDMainTabBar sharedDDMainTabBar].playingMusicSlider.musicSider.value = 0;
                    [[DDMainTabBar sharedDDMainTabBar].playingMusicBtn.layer pauseAnimate];
                    [DDMainTabBar sharedDDMainTabBar].playingMusicSlider.currentTimeLabel.text = @"00:00";
                    [DDMainTabBar sharedDDMainTabBar].statrOrStopBtn.selected = NO;
                    
                    
                }
                break;
            case SINGLELOOPPLAY:{
                self.currentPlayModel = self.playMusicList[self.currentPlayMusicIndex];
                [self refreshUI:self.currentPlayModel];
                [self playMusic:self.currentPlayModel];
            }
                break;
            case ALLLOOPPLAY:{
                if (self.playMusicList.count>self.currentPlayMusicIndex+1) {
                    self.currentPlayModel = self.playMusicList[self.currentPlayMusicIndex+1];
                    [self refreshUI:self.currentPlayModel];
                    [self playMusic:self.currentPlayModel];
                    self.currentPlayMusicIndex+=1;
                }else{
                    self.currentPlayMusicIndex = -1;
                    [self playNextOrUpBtnClicked:self.nextBtn];
                }
                break;
            }
            case RANDOMPLAY:{
                int randomMusicIndex = arc4random()%self.playMusicList.count;
                self.currentPlayModel = self.playMusicList[randomMusicIndex];
                [self refreshUI:self.currentPlayModel];
                [self playMusic:self.currentPlayModel];
                self.currentPlayMusicIndex = randomMusicIndex;
                
            }
                break;
            default:
                break;
        }
        
    }else {
    //上一首按钮
        if (self.currentPlayMusicIndex-1>=0) {
            self.currentPlayModel = self.playMusicList[self.currentPlayMusicIndex-1];
            [self refreshUI:self.currentPlayModel];
            [self playMusic:self.currentPlayModel];
            self.currentPlayMusicIndex-=1;
        }else if (self.player.status ==AVPlayerStatusReadyToPlay){
        
        }else{
            self.progressView.musicSider.value = 0;
            self.playMusicTimer.fireDate = [NSDate distantFuture];
            self.progressView.currentTimeLabel.text = @"00:00";
            self.stopOrPauseBtn.selected  = NO;
            [DDMainTabBar sharedDDMainTabBar].playingMusicSlider.musicSider.value = 0;
            [[DDMainTabBar sharedDDMainTabBar].playingMusicBtn.layer pauseAnimate];
            [DDMainTabBar sharedDDMainTabBar].playingMusicSlider.currentTimeLabel.text = @"00:00";
            [DDMainTabBar sharedDDMainTabBar].statrOrStopBtn.selected = NO;
        }
    }
}
#pragma mark 滑条的回调方法
- (void)sliderMusicProgress{
    if (self.stopOrPauseBtn.selected ==YES) {
         [self.player seekToTime:CMTimeMake(self.progressView.musicSider.value*[self.currentPlayModel.seconds doubleValue], 1)];
    }
}
#pragma mark 工具按钮的代理回调方法
-(void)muneBarselected:(NSInteger)index{
    switch (index) {
        case 0:
            //歌曲播放方式按钮
            [self playMusicStyleBtnClicked];
            break;
        case 1:
            //收藏按钮
            [self likeOrDislikeBtnClicked];
            break;
        case 2:
            //弹幕按钮
            break;
        case 3:
            //下载按钮
            break;
        case 4:
            //设置按钮
            break;
        case 5:
            //礼物按钮
            break;
        default:
            break;
    }
}
#pragma mark 工具栏按钮歌曲播放方式按钮的回调方法
- (void)playMusicStyleBtnClicked{
    static int i = 0 ;
    if (i>3) {
        i = 0;
    }
    switch (i) {
        case 0:{
            self.playSequence = DEFAULT;
            [[DDAlertView sharedDDAlertView] showAlertViewWithTitle:@"默认"];
            
        }
            
            break;
        case 1:{
            self.playSequence = SINGLELOOPPLAY;
            [[DDAlertView sharedDDAlertView] showAlertViewWithTitle:@"单曲循环"];
            
        }
            break;
        case 2:{
            self.playSequence = ALLLOOPPLAY;
            [[DDAlertView sharedDDAlertView] showAlertViewWithTitle:@"循环播放"];
        }
            break;
        case 3:{
            self.playSequence = RANDOMPLAY;
            [[DDAlertView sharedDDAlertView] showAlertViewWithTitle:@"随机播放"];
        }
            break;
            
        default:
            break;
    }
    i++;
}
#pragma mark 工具栏按钮歌曲收藏按钮的回调方法
- (void)likeOrDislikeBtnClicked{
    CMuneItem* item = self.musicToolBar.items[1];
    item.selected = !item.selected;
    if (item.selected ==YES) {
        //收藏
        [[DDAlertView sharedDDAlertView] showAlertViewWithTitle:@"收藏成功"];
        
    }else{
    //取消收藏
        [[DDAlertView sharedDDAlertView] showAlertViewWithTitle:@"取消收藏成功"];

    }
}
#pragma mark 播放列表kvo回调方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    //playMusicList的kvo
    if ([keyPath isEqualToString:@"playMusicList"]) {
        

    if (self.playMusicList <= 0) {
        return;
    }
    self.currentPlayModel = self.playMusicList.firstObject;
    self.currentPlayMusicIndex = 0;
    [self refreshUI:self.currentPlayModel];
        [self playMusic:self.currentPlayModel];
    }
    //歌曲缓冲的kvo
    if ([keyPath isEqualToString:@"loadedTimeRanges"]) {
//        AVPlayerItem* songItem = object;
//        NSArray* arr = songItem.loadedTimeRanges;
//        CMTimeRange timeRange = [arr.firstObject CMTimeRangeValue];//本次缓冲的时间范围
//        NSTimeInterval totalBuffer = CMTimeGetSeconds(timeRange.start)+CMTimeGetSeconds(timeRange.duration);//缓冲总长度
//        NSLog(@"共缓冲%.2f",totalBuffer);
        
    }
    //歌曲加载状态
    if ([keyPath isEqualToString:@"status"]) {
        if ([keyPath isEqualToString:@"status"]) {
         switch (self.player.status) {
                case AVPlayerStatusUnknown:
//                   NSLog(@"KVO：未知状态，此时不能播放");
                    break;
                case AVPlayerStatusReadyToPlay:
//                     NSLog(@"KVO：准备完毕，可以播放");
                    break;
                case AVPlayerStatusFailed:
//                     NSLog(@"KVO：加载失败，网络或者服务器出现问题");
                    break;
                default:
                    break;        
            }
        }
    }
}
#pragma mark 刷新ui
- (void)refreshUI:(DDMusicModel*)model{
    self.songNameLabel.text = model.songname;
    self.singerNameLabel.text = model.singername;
   [self.bgImageView sd_setImageWithURL:[NSURL URLWithString:model.albumpic_big] placeholderImage:nil options:SDWebImageLowPriority];
     self.bgImageView.contentMode = UIViewContentModeScaleAspectFill;
 
    
    
//歌曲时长
    NSInteger time = [model.seconds integerValue];
    NSInteger min =  time/60;
    NSInteger sec = time%60;
    NSString* musicTimeStr = [NSString stringWithFormat:@"%.2ld:%.2ld",(long)min,(long)sec];
    self.progressView.musicTimeLabel.text = musicTimeStr;
    [DDMainTabBar sharedDDMainTabBar].playingMusicNameLabel.text = model.songname;
    [DDMainTabBar sharedDDMainTabBar].playingMusicSingerLabel.text = model.singername;
    [[DDMainTabBar sharedDDMainTabBar].playingMusicBtn sd_setImageWithURL:[NSURL URLWithString:model.albumpic_big] forState:(UIControlStateNormal) placeholderImage:[UIImage imageNamed:@"Music"]];

}

#pragma mark 播放音乐
- (void)playMusic:(DDMusicModel*)model{
     [self.item removeObserver:self forKeyPath:@"loadedTimeRanges"];
    [self.item removeObserver:self forKeyPath:@"status"];
 NSURL* url = [NSURL URLWithString:model.url];//得到url地址
        
        if (!url) {
            return;
        }
       self.item = [AVPlayerItem playerItemWithURL:url];
#pragma mark 歌曲缓冲kvo
    [self.item addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil];
#pragma mark 歌曲加载状态kvo 
    [self.item addObserver:self forKeyPath:@"status" options:(NSKeyValueObservingOptionNew) context:nil];
        self.player = [AVPlayer playerWithPlayerItem:self.item];
        [self.player play];
        self.stopOrPauseBtn.selected = YES;
    [DDMainTabBar sharedDDMainTabBar].statrOrStopBtn.selected = YES;
    [[DDMainTabBar sharedDDMainTabBar].playingMusicBtn.layer resumeAnimate];
    if (!self.playMusicTimer) {
        self.playMusicTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(monitoringMusicProgress) userInfo:nil repeats:YES];
    }
    
}
#pragma markplay MusicTimer回调方法用于实时切换滑条进度
- (void)monitoringMusicProgress{
CGFloat musicCurrentTime = CMTimeGetSeconds(self.item.currentTime);//获取当前音乐的进度
    //更新进度条的进度
    self.progressView.musicSider.value = musicCurrentTime/[self.currentPlayModel.seconds doubleValue];
    //更新进度条左边的时间
    NSInteger min = musicCurrentTime/60;
    NSInteger sec = (NSInteger)musicCurrentTime%60;
    self.progressView.currentTimeLabel.text = [NSString stringWithFormat:@"%.2ld:%.2ld",min,sec];
    
    [DDMainTabBar sharedDDMainTabBar].playingMusicSlider.musicSider.value  = self.progressView.musicSider.value;
    

}
#pragma mark 单例
static  DDPlayMusicViewController* instance = nil;
+ (instancetype)sharedDDPlayMusicViewController{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[DDPlayMusicViewController alloc]init];
    });
    return instance;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone{
    @synchronized (self) {
        if (!instance){
            instance = [super allocWithZone:zone];
        }
    }
    return instance;
    
}
- (id)copyWithZone:(NSZone *)zone{
    return self;
}

#pragma mark 状态栏
- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
#pragma mark 通知相关的
- (void)registerCenter{
//当播放音乐结束后的通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.item];
}
#pragma mark 通知的回调方法
- (void)playbackFinished:(NSNotification*)notification{
    if (self.playSequence == DEFAULT && self.currentPlayMusicIndex  == self.playMusicList.count-1) {
        self.progressView.musicSider.value = 0;
        self.playMusicTimer.fireDate = [NSDate distantFuture];
        self.progressView.currentTimeLabel.text = @"00:00";
        self.stopOrPauseBtn.selected = NO;
        [DDMainTabBar sharedDDMainTabBar].playingMusicSlider.musicSider.value = 0;
        [[DDMainTabBar sharedDDMainTabBar].playingMusicBtn.layer pauseAnimate];
        [DDMainTabBar sharedDDMainTabBar].playingMusicSlider.currentTimeLabel.text = @"00:00";
        [DDMainTabBar sharedDDMainTabBar].statrOrStopBtn.selected = NO;
        
    }else{
        [self playNextOrUpBtnClicked:self.nextBtn];
    
    }
    
}
#pragma mark 播放器使用上拉菜单对app播放器进行控制
//开始接受远程控制
- (void)acceptlongRangeControl{
    [[UIApplication sharedApplication]beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];
    
}
//接触远程控制
- (void)touchLongRangeControl{
    [[UIApplication sharedApplication]endReceivingRemoteControlEvents];
    [self resignFirstResponder];
    
}
//重写父类成为响应者
- (BOOL)canBecomeFirstResponder{
    return YES;
    
}
//对远程控制事件作出相应的操作
- (void)remoteControlReceivedWithEvent:(UIEvent *)event{
    if (event.type==UIEventTypeRemoteControl) {
        if (event.subtype ==  UIEventSubtypeRemoteControlPause) {//暂停
            [self playNextOrUpBtnClicked:self.stopOrPauseBtn];
        }else if (event.subtype==  UIEventSubtypeRemoteControlNextTrack){//下一首
            [self playNextOrUpBtnClicked:self.nextBtn];
        }else if (event.subtype== UIEventSubtypeRemoteControlPreviousTrack){//上一首
            [self playNextOrUpBtnClicked:self.upBtn];
        }else if (event.subtype== UIEventSubtypeRemoteControlPlay){//播放
            [self playNextOrUpBtnClicked:self.stopOrPauseBtn];
        }
    }
    
}
#pragma mark 歌曲摇一摇切换到下一首歌曲
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
//    NSLog(@"开始摇一摇");
    [[DDmediaPlayer shareDDmediaPlayer] shake];//开启摇一摇震动
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{

    [self playNextOrUpBtnClicked:self.nextBtn];
    
}
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{
//    NSLog(@"取消摇一摇");
}

@end
